//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using CommunityServer.Components;
using CommunityServer.ControlPanel.Controls;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using HelpIcon = CommunityServer.ControlPanel.Controls.HelpIcon;
using ResourceControl = CommunityServer.ControlPanel.Controls.ResourceControl;
using ResourceLinkButton = CommunityServer.ControlPanel.Controls.ResourceLinkButton;
using StatusMessage = CommunityServer.ControlPanel.Controls.StatusMessage;

namespace CommunityServer.ControlPanel.Settings
{
	/// <summary>
	/// Summary description for DisableApplications.
	/// </summary>
	public class DisableApplications : BaseSettingsPage
	{

		#region Members

		protected YesNoRadioButtonList ForumsDisabled;
		protected YesNoRadioButtonList BlogsDisabled;
		protected YesNoRadioButtonList GalleriesDisabled;
		protected YesNoRadioButtonList FeedReaderDisabled;
		protected YesNoRadioButtonList BlogRollerDisabled;

		protected StatusMessage Status;
		protected ControlPanelSelectedNavigation SelectedNavigation1;
		protected ResourceControl RegionTitle;
		protected MPContent DescriptionRegion;
		protected HelpIcon Helpicon2;
		protected ResourceControl ResourceControl7;
		protected HelpIcon Helpicon1;
		protected ResourceControl ResourceControl1;
		protected HelpIcon Helpicon3;
		protected ResourceControl ResourceControl3;
		protected HelpIcon Helpicon4;
		protected ResourceControl Resourcecontrol5;
		protected HelpIcon Helpicon5;
		protected ResourceControl Resourcecontrol2;
		protected HelpIcon Helpicon6;
		protected ResourceControl Resourcecontrol4;
		protected MPContent TaskRegion;
		protected MPContainer MPContainer;
		protected ResourceLinkButton SaveButton;


		#endregion

		override protected void OnInit(EventArgs e)
		{

			this.SaveButton.Click += new EventHandler(SaveButton_Click);
			this.Load += new EventHandler(this.Page_Load);

			base.OnInit(e);
		}


		private void Page_Load(object sender, EventArgs e)
		{
			if ( !Page.IsPostBack ) 
			{
				this.Bind();
			}
		}

		protected void Bind()
		{
			SiteSettings settings = CSContext.Current.SiteSettings;
			Status.Visible = false;
			this.SaveButton.Text = ResourceManager.GetString( "Save" );

            ApplicationSet appSet = ApplicationSet.Applications;
            foreach(CSApplicationData data in appSet)
            {
                if(data != null)
                {
                    YesNoRadioButtonList yn = SaveButton.Parent.FindControl(data.ApplicationType.ToString()) as YesNoRadioButtonList;
                    if(yn != null)
                    {
                        yn.SelectedValue = !data.Enabled;
                    }

                }
            }


		}

		private void InitializeComponent()
		{
		
		}

		private void SaveButton_Click(object sender, EventArgs e) 
		{
//			SiteSettings settings = CSContext.Current.SiteSettings;
//
//			// First, see if any of the apps have been enabled/disabled
//			if((settings.ForumsDisabled != ForumsDisabled.SelectedValue) ||
//				(settings.BlogsDisabled != BlogsDisabled.SelectedValue) ||
//				(settings.GalleriesDisabled != GalleriesDisabled.SelectedValue) ||
//				(settings.FeedReaderDisabled != FeedReaderDisabled.SelectedValue) ||
//				(settings.BlogRollerDisabled != BlogRollerDisabled.SelectedValue))
//				enabledAppChanged = true;
//
//			SaveSettings( settings );

            ApplicationSet appSet = ApplicationSet.Applications;
            
            foreach(CSApplicationData data in appSet)
            {
            
                if(data != null)
                {
            
                    YesNoRadioButtonList yn = SaveButton.Parent.FindControl(data.ApplicationType.ToString()) as YesNoRadioButtonList;
                    if(yn != null)
                    {
                        data.SetEnabledStatus(!yn.SelectedValue);
                    }
                }
            }

			if(Page.IsValid)
			{
				// Refresh the SiteUrl data to remove the links to the disabled apps
				
					SiteUrls.ForceRefresh();
					CSCache.RemoveByPattern("MainNavigationUrls-");

					// Remove the context for the NavigationMenu and have it redatabind
					// For some reason, the first separator still shows.
					Context.Items.Remove("CommunityServer.Controls.NavigationMenu");
					Page.DataBind();
				

				Status.Success = true;
				Status.ResourceName = "Admin_SiteSettings_StatusSuccess";

				DataBind();
			}
			else
			{
				Status.Success = false;
				Status.ResourceName = "Admin_SiteSettings_StatusFailed";
			}

			Status.Visible = true;
		}
	}
}
